import javafx.scene.effect.Light;
import org.junit.Test;

import java.io.*;

/**
 * @auther yangdong
 * @create 2020-04-14
 */
public class InputStreamOutputStreamTest {
    /**
     * 使用字节流处理文本（.txt）文件可能出现乱码，因为字节流是以字节为单位进行处理的，汉字占用3个字节，可能在处理过程中出现3个字节不连续导致乱码
     * .txt.java.c.c++都是文本文件
     */
    @Test
    public void fileInputStreamTest(){
        File file=new File("src/hello.txt");
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(file);
            byte[]b=new byte[5];
            int len;
            while ((len=fis.read(b))!=-1){
//                for (int i = 0; i <len ; i++) {
//////                        System.out.print(b[i]);
//////
//////                }
                String str=new String(b,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void copy(){
        Long start=System.currentTimeMillis();
        copyFile("G:\\BaiduNetdiskDownload\\尚硅谷\\软件\\powerdesigner1029.zip","H:\\powerdesigner1029.zip");
        Long end=System.currentTimeMillis();
        System.out.println("耗时："+(end-start));
    }
    public void copyFile(String src,String dist){
        File file=new File(src);
        File file1=new File(dist);
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream(file);
            fos=new FileOutputStream(file1);
            byte[] b=new byte[1024];
            int len;
            while ((len=fis.read(b))!=-1){
                fos.write(b,0,len);
            }
            System.out.println("wancheng");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
